package com.alibaba.dongbao.utils.jwt;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;

/**
 * @Author: Mr.ZJW
 * @Date: 2022-04-08 10:19
 * @Description: jwt工具类
 */
public class JwtUtil {

    //盐
    private static final String SECRET = "!Q@W3e4r%T^Y";
    //过期时间
    private static final long EXPIRED_TIME = 24 * 60 * 60 * 1000;

    /**
     * @Author: Mr.ZJW
     * @Description: 生成token
     * @Date: 2022/4/8 10:31
     **/
    public static String createToken(String subject) {
        String token = Jwts.builder().setSubject(subject)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRED_TIME))
                .signWith(SignatureAlgorithm.HS256, SECRET)
                .compact();
        return token;
    }

    /**
     * @Author: Mr.ZJW
     * @Description: 解析token
     * @Date: 2022/4/8 10:39
     **/
    public static String parseToken(String token) {
        Claims body = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        return body.getSubject();
    }

}
